define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'vip/jiangli/index' + location.search,
                    //add_url: 'vip/jiangli/add',
                    //edit_url: 'vip/jiangli/edit',
                    //edit_all_url: 'vip/jiangli/edit_all',
                    del_url: 'vip/jiangli/del',
                    //multi_url: 'vip/jiangli/multi',
                    table: 'vip_jiangli',
                }
            });

            var table = $("#table");

            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                //console.log(data.extend.money);
                //这里我们手动设置底部的值
                $("#money_box").text(data.extend.money);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                searchFormVisible: true,
                showToggle: false,
                showColumns: false,
                showExport: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'username', title: __('Username')},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'level', title: '当前等级'},
                        {
                            //field: 'level',
                            field: 'msg',
                            title: __('Level'),operate:false
                            //formatter: function(value){
                            //    // 等级变化: 0级 => 1级
                            //    var oldLevel = value - 1;
                            //    return "等级变化: "+oldLevel+"级 => "+value+"级";
                            //}
                        },
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'month', title: __('Month')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 批量修改js
            $(".btn-edit_all").click(function(){
                var ids = Table.api.selectedids(table);
                layer.confirm('确定要派发选中吗？', {
                    btn: ['确定','取消'] //按钮
                }, function(){
                    $.ajax({
                        url:"vip/jiangli/edit_all/ids/"+ids,
                        dataType:"json",
                        type:"get",
                        //data:{"ids":ids},
                        success:function(re){
                            if(re.status == 200){
                                layer.msg(re.msg);
                                table.bootstrapTable('refresh', {});
                            } else {
                                layer.msg(re.msg);
                            }
                        }
                    });
                }, function(){
                    layer.msg('已取消', {icon: 2});
                });

            });

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});